package com.erp.erp_ui.money.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.enums.DictionaryLxEnum;
import com.erp.enums.DictionaryTypeEnum;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.money.req.*;
import com.erp.erp_entitys.money.rsp.*;
import com.erp.erp_entitys.warehouse.req.DictionaryInfoReq;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.AddPaymentReq;
import com.erp.erp_ui.money.req.ImportPurchaseReq;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.money.service.PaymentService;
import com.erp.erp_ui.money.vo.*;
import com.erp.erp_ui.warehouse.vo.DictionaryInfoVo;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : PaymentController
 * @Description : 资金管理-付款控制层
 * @Author : lst
 * @Date: 2020-12-29 19:59
 */
@Controller
@RequestMapping(value = "payment")
public class PaymentController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentController.class);

    /**
     * 付款业务逻辑层接口
     */
    private final PaymentService paymentService;

    @Autowired
    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    /**
     * 资金管理-付款-获取分页列表
     *
     * @param req 查询参数
     * @return 获取分页列表
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(PaymentPageListReq req) {
        try {
            LOGGER.info("资金管理-付款-获取分页列表 req=" + req.toString());
            PageInfo<PaymentPageListVo> pageInfo = paymentService.getListByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取分页列表异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 资源管理-付款-修改标记颜色
     * @param req 入参实体
     * @return 修改结果
     */
    @PostMapping("/updateMarkColor")
    @ResponseBody
    public R updateMarkColor(@RequestBody OperationReq req) {
        try {
            LOGGER.info("资源管理-付款-修改标记颜色 req=" + req);
            paymentService.updateMarkColor(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("资源管理-付款-修改标记颜色异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 资金管理-付款-获取采购信息
     *
     * @param req 查询参数
     * @return 采购信息
     */
    @GetMapping("/getImportPurchaseByPage")
    @ResponseBody
    public R getImportPurchaseByPage(ImportPurchaseReq req) {
        try {
            LOGGER.info("资金管理-付款-获取采购信息 req=" + req.toString());
            PageInfo<ImportPurchaseVo> pageInfo = paymentService.getImportPurchaseByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取采购信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 资金管理-付款-获取出运明细信息
     *
     * @param req 查询参数
     * @return 出运明细信息
     */
    @GetMapping("/getImportCarryDetail")
    @ResponseBody
    public R getImportCarryDetail(ImportCarryDetailInfoReq req) {
        try {
            LOGGER.info("资金管理-付款-获取出运明细信息 req=" + req.toString());
            List<ImportCarryDetailVo> list = paymentService.getImportCarryDetail(req);
            return R.build(list);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取出运明细信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 资金管理-付款-获取出运明细信息
     *
     * @param req 查询参数
     * @return 出运明细信息
     */
    @GetMapping("/getImportCarryDetailByPage")
    @ResponseBody
    public R getImportCarryDetailByPage(ImportCarryDetailInfoReq req) {
        try {
            LOGGER.info("资金管理-付款-获取出运明细信息 req=" + req.toString());
            PageInfo<ImportCarryDetailVo> pageInfo = paymentService.getImportCarryDetailByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取出运明细信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取字典信息-付款类型
     *
     * @return 付款类型
     */
    @GetMapping("/getPaymentTypeInfo")
    @ResponseBody
    public R getReceivePaymentTypeInfo() {
        try {
            LOGGER.info("资金管理-付款-获取字典信息-付款类型信息");
            //获取所有资源仓库字典信息
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //结算相关
            dictionaryInfosReq.setType(DictionaryTypeEnum.SETTLEMENT.getId());
            //付款类型
            dictionaryInfosReq.setLx(DictionaryLxEnum.PAYMENT_TYPE.getId());
            List<DictionaryInfoVo> dictionaryInfos = paymentService.getPaymentTypeInfo(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取字典信息-付款类型信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取银行信息
     * @param code 供应商编码
     * @return  银行信息
     */
    @GetMapping("/getBankInfo")
    @ResponseBody
    public R getBankInfo(String code) {
        try {
            LOGGER.info("资金管理-付款-获取银行信息：code="+code);
            List<BankInfoRsp> bankList = paymentService.getBankInfo(code);
            return R.build(bankList);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取银行信异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取供应商信息
     *
     * @return 供应商
     */
    @GetMapping("/getSupplierInfo")
    @ResponseBody
    public R getSupplierInfo() {
        try {
            LOGGER.info("资金管理-付款-获取供应商信息");
            List<SupplierInfoRsp> supplierInfo = paymentService.getSupplierInfo();
            return R.build(supplierInfo);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-获取供应商信异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增、修改
     * @param req 新增、修改入参
     * @return 新增、修改结果
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@RequestBody AddPaymentReq req){
        try {
            LOGGER.info("资金管理-付款-新增、修改 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return paymentService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("资金管理-付款-新增、修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 实付新增
     * @param req 实付新增入参
     * @return 实付新增结果
     */
    @PostMapping("/addActually")
    @ResponseBody
    public R addActually(@RequestBody AddPaymentReq req){
        try {
            LOGGER.info("资金管理-付款-实付新增 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return paymentService.addActually(req, name, code);
        }catch (Exception e){
            LOGGER.info("资金管理-付款-实付新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 退款新增
     * @param req 退款新增入参
     * @return 退款新增
     */
    @PostMapping("/addRefund")
    @ResponseBody
    public R addRefund(@RequestBody List<AddPaymentReq> req){
        try {
            LOGGER.info("资金管理-付款-退款新增 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return paymentService.addRefund(req, name, code);
        }catch (Exception e){
            LOGGER.info("资金管理-付款-退款新增异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 资金管理-付款-退款-获取付款数据
     *
     * @param id 付款主表数据id
     * @return 付款数据
     */
    @GetMapping("/getRefundInfo")
    @ResponseBody
    public R getPaymentInfo(String id) {
        try {
            LOGGER.info("资金管理-付款-退款-获取付款数据 id=" + id);
            List<PaymentInfoVo> voList = paymentService.getPaymentInfo(id);
            return R.build(voList);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-退款-获取付款数据异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 详情
     * @param id 详情id
     * @return 详情信息
     */
    @GetMapping("/detail")
    @ResponseBody
    public R detail(Integer id){
        try {
            LOGGER.info("资金管理-付款-详情：id=" + id);
            PaymentInfoVo detailVo = paymentService.detail(id);
            return R.build(detailVo);
        }catch (Exception e){
            LOGGER.info("资金管理-付款-详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除
     * @param req 删除条件
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody OperationReq req) {
        try {
            LOGGER.info("资金管理-删除 req=" + req);
            paymentService.delete(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("资金管理-删除异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 数据操作接口
     * 1.提交审批 2.审批通过 3.审批退回 4.完成 5.还原
     * @param req 操作类型
     * @return 操作结果
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req) {
        try {
            LOGGER.info("资金管理-付款-数据操作接口 req=" + req);
            return paymentService.operation(req);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }






    //************************付款统计***********************************

    /**
     * 付款情况统计
     * @param req
     * @return
     */
    @RequestMapping("paymentStatistics1ListByPage")
    @ResponseBody
    public R paymentStatistics1ListByPage(PaymentStatistics1Req req){
        try {
            LOGGER.info("付款情况统计");
            PageInfo<PaymentStatistics1Rsp> pageInfo=paymentService.PaymentStatisticsPageInfo(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            LOGGER.error("费用->付款情况统计功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 付款情况统计-导出
     * @param req
     * @return
     */
    @RequestMapping("paymentStatistics1ListExport")
    @ResponseBody
    public R paymentStatistics1ListExport(PaymentStatistics1Req req){
        try {
            LOGGER.info("付款情况统计-导出");
            JSONObject info=paymentService.getStatisticsTwoPurchaseInfoExport(req);
            return R.build(info);
        } catch (Exception e) {
            LOGGER.error("费用->付款情况统计-导出功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 采购预付款统计
     *
     * @param req 查询参数
     * @return 采购预付款统计
     */
    @GetMapping(value = "statistics/getStatisticsPurchasePrepaymentsByPage")
    @ResponseBody
    public R getStatisticsPurchasePrepayments(PaymentStatistics1Req req) {
        try {
            LOGGER.info("资金管理-付款-采购预付款统计");
            PageInfo<PaymentStatistics1Rsp> pageInfo = paymentService.getStatisticsPurchasePrepayments(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-采购预付款统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 采购预付款统计-导出
     * @param req
     * @return
     */
    @RequestMapping("export/getStatisticsPurchasePrepaymentExport")
    @ResponseBody
    public R getStatisticsPurchasePrepaymentExport(PaymentStatistics1Req req){
        try {
            LOGGER.info("采购预付款统计-导出");
            JSONObject jsonObject = paymentService.getStatisticsPurchasePrepaymentExport(req);
            return R.build(jsonObject);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-采购预付款统计-导出异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 出货金额统计-分页
     * @param req
     * @return
     */
    @RequestMapping("paymentShipmentAmountStatisticsByPage")
    @ResponseBody
    public R paymentShipmentAmountStatisticsByPage(PaymentShipmentAmountStatisticsReq req){
        try {
            LOGGER.info("出货金额统计-分页");
            PageInfo<PaymentShipmentAmountStatisticsRsp> pageInfo=paymentService.paymentShipmentAmountPageInfo(req);
            return R.build(0,"",pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception e) {
            LOGGER.error("付款->出货金额统计功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 出货金额统计-导出
     * @param req
     * @return
     */
    @RequestMapping("paymentShipmentAmountStatisticsExport")
    @ResponseBody
    public R paymentShipmentAmountStatisticsExport(PaymentShipmentAmountStatisticsReq req){
        try {
            LOGGER.info("出货金额统计-导出");
            JSONObject info=paymentService.ExportPaymentShipmentAmount(req);
            return R.build(info);
        } catch (Exception e) {
            LOGGER.error("付款->出货金额统计-导出功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }



    /**
     * 采购付款统计--分页
     */
    @GetMapping(value = "statistics/purchasePaymentStatisticsByPage")
    @ResponseBody
    public R purchasePaymentStatisticsByPage(purchasePaymentStatisticsReq req) {
        try {
            LOGGER.info("采购付款统计");
            PageInfo<purchasePaymentStatisticsRes> pageInfo = paymentService.purchasePaymentStatistics(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("采购付款统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 采购付款统计-导出
     * @param req
     * @return
     */
    @RequestMapping("purchasePaymentStatisticsExport")
    @ResponseBody
    public R purchasePaymentStatisticsExport(purchasePaymentStatisticsReq req){
        try {
            LOGGER.info("采购付款统计-导出");
            JSONObject info=paymentService.purchasePaymentStatisticsExport(req);
            return R.build(info);
        } catch (Exception e) {
            LOGGER.error("采购付款统计-导出功能异常",e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过采购单号获取采购样品已做付款数量
     *
     * @param purchaseNo 采购单号
     * @return 采购样品已做付款数量
     */
    @GetMapping("/getResourceSampleCountByPurchaseNo")
    @ResponseBody
    public R getResourceSampleCountByPurchaseNo(String purchaseNo) {
        try {
            LOGGER.info("资金管理-付款-通过采购单号获取采购样品已做付款数量 purchaseNo=" + purchaseNo);
            int count = paymentService.getResourceSampleCountByPurchaseNo(purchaseNo);
            return R.build(count);
        } catch (Exception e) {
            LOGGER.info("资金管理-付款-通过采购单号获取采购样品已做付款数量异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
}
